<?php
namespace app\admin\controller;
use think\facade\View;
use think\facade\Db;
use app\common\model\Ads;
use app\common\model\User;
use app\common\model\Order;
class Index extends Base{
	public function index(){		
	    return View::fetch('Index/index');
	}

	public function welcome(){	
// 	    $ads = Ads::where([])->field('account_id,id')->select()->toArray();
//         View::assign('ads', $ads);
//         $lingchen = strtotime(date('Y-m-d'));
//         #今日注册用户
//         $today_register= User::where('addtime','>',$lingchen)->where('click_id','>',0)->count();
//         #今日消费金额用户
//         $today_new_consume = Order::where('addtime','>',$lingchen)->where('price','>',0)->where('ad_id','>',0)->where('click_id','>',0)->count();
//         #当日消费总金额
//         $today_comsume = Order::where('addtime','>',$lingchen)->where('price','>',0)->where('ad_id','>',0)->where('click_id','>',0)->sum('price');
//         #总消费金额
//         $ad_total = Order::where('price','>',0)->where('ad_id','>',0)->where('click_id','>',0)->sum('price');
//         View::assign('today_register',$today_register);
//         View::assign('today_new_consume',$today_new_consume);
//         View::assign('today_comsume',$today_comsume);
//         View::assign('ad_total',$ad_total);
// 		return View::fetch('Index/welcome');
	}

    public function ads(){
        $list = Ads::getAllList([],'*','id desc');
        // foreach ($list as $key => &$val){
        //     $val['consume'] = \app\common\model\Order::where('ad_id',$val['ads'])->where('status',2)->where('click_id','<>','')->whereRaw('pay_time > addtime')->sum('total_price');
        // }
        View::assign('list',$list);
        return View::fetch('Index/ads');
    }
    
    public function ads_add(){
        if(request()->isPost()){
            $data = input('post.');
            // 腾讯广告创建数据源
            $account_id = $data['account_id'];
            $access_token = $data['access_token'];
            $result = $this->user_action_sets_add($account_id,$access_token);
//            $result = '{"code":0,"data":{"user_action_set_id":1201361675},"message":""}';
            $result = json_decode($result,true);
            if ($result['code'] != 0){
                return $this->renderError($result['message']);
            }
            $data['user_action_set_id'] = $result['data']['user_action_set_id'];

            $res = Ads::create($data);
            if($res){
                return $this->renderSuccess('添加成功');
            }else{
                return $this->renderError('添加失败');
            }
        }
        return View::fetch('Index/ads_add');
    }
    
    private function user_action_sets_add($account_id,$access_token)
    {
        $interface = 'user_action_sets/add';
        $url = 'https://api.e.qq.com/v1.1/' . $interface;

        $common_parameters = array (
            'access_token' => $access_token,
            'timestamp' => time(),
            'nonce' => md5(uniqid('', true))
        );

        $parameters = array (
            'account_id' => $account_id,
            'type' => 'WEB',
            'name' => 'webuser_action_set',
            'description' => '',
            'usages' =>
                array (
                ),
        );

        $parameters = json_encode($parameters);
        $request_url = $url . '?' . http_build_query($common_parameters);

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $request_url);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            "Content-Type:application/json"
        ));
        $response = curl_exec($curl);
        if (curl_error($curl)) {
            $error_msg = curl_error($curl);
            $error_no = curl_errno($curl);
            curl_close($curl);
            throw new \Exception($error_msg, $error_no);
        }
        curl_close($curl);
        return $response;
    }



}